package com.open.thinks.todo.system.controller;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.open.thinks.todo.system.common.EnumContentableOption;
import com.open.thinks.todo.system.common.IdGenerator;
import com.open.thinks.todo.system.common.WebUtils;
import com.open.thinks.todo.system.domain.Task;
import com.open.thinks.todo.system.domain.helper.TaskPriority;
import com.open.thinks.todo.system.domain.helper.TaskRemider;
import com.open.thinks.todo.system.domain.helper.TaskStatus;
import com.open.thinks.todo.system.domain.helper.TaskType;
import com.open.thinks.todo.system.service.TaskService;

@Controller
@RequestMapping("/task")
public class TaskController {

	@Autowired
	TaskService taskService;

	@RequestMapping("/list")
	public String index(Model model) {
		model.addAttribute("tasks", taskService.getTasks(null));
		return "task-main";
	}

	@RequestMapping("/add")
	public ModelAndView add(Task task, Model model, HttpSession session) {
		task.setAssignee(WebUtils.getCurrentUser(session));
		model.addAttribute("types", EnumContentableOption.options(TaskType.class));
		model.addAttribute("status", EnumContentableOption.options(TaskStatus.class));
		model.addAttribute("priorities", EnumContentableOption.options(TaskPriority.class));
		model.addAttribute("reminders", EnumContentableOption.options(TaskRemider.class));
		return new ModelAndView("task-add");
	}

	@RequestMapping("/add/save")
	public ModelAndView add_save(@Valid Task task, BindingResult result, Model model, HttpSession session) {
		if (result.hasErrors()) {
			return add(task, model, session);
		}
		String task_id = IdGenerator.getGenerator(Task.class).generator();
		task.setTask_id(task_id);
		task.setCreateby(WebUtils.getCurrentUser(session));
		taskService.store(task);
		return new ModelAndView("redirect:/task/list" + WebUtils.ACCESS_SUFFIX);
	}

	@RequestMapping("/{task_id}/edit")
	public String edit(@PathVariable String task_id, Model model) {
		Task task = taskService.getTaskById(task_id);
		model.addAttribute("task", task);
		model.addAttribute("types", EnumContentableOption.options(TaskType.class));
		model.addAttribute("status", EnumContentableOption.options(TaskStatus.class));
		model.addAttribute("priorities", EnumContentableOption.options(TaskPriority.class));
		model.addAttribute("reminders", EnumContentableOption.options(TaskRemider.class));
		return "task-edit";
	}

	@RequestMapping("/edit/save")
	public ModelAndView edit_save(Task task, Model model) {
		taskService.update(task);
		return new ModelAndView("redirect:/task/list" + WebUtils.ACCESS_SUFFIX);
	}

	@RequestMapping("/delete")
	public ModelAndView delete(Task task) {
		taskService.delete(task);
		return new ModelAndView("redirect:/task/list" + WebUtils.ACCESS_SUFFIX);
	}
}
